﻿using System.Collections.Generic;
using CodeParser.Tokens;

namespace CodeParser.Expressions
{
    public class PassParser
        : ExpressionParser
    {

        public override Expression Parse(Token[] tokens, int index)
        {
            var firstToken = tokens[index];
            if (firstToken.Type == TokenType.Identifier && firstToken.Value == "pass")
            {
                var i = index + 1;
                var expression = new Pass {PassToken = firstToken, StringIndex = firstToken.Index};
                expression.Add(new SingleToken() { Token = firstToken});
                if (tokens[i].Type == TokenType.Identifier)
                {
                    expression.Identifier = tokens[i++];
                    expression.Add(new SingleToken() {Token = expression.Identifier});
                }
                expression.CodeBlock = CodeBlockParser.Parse(tokens, i);
                if (expression.CodeBlock != null)
                {
                    expression.Add(expression.CodeBlock);
                    expression.TokenLength = i + expression.CodeBlock.TokenLength - index;
                }
                else return null;

                var lastToken = tokens[index + expression.TokenLength - 1];
                expression.StringLength = lastToken.Index + lastToken.Value.Length - expression.StringIndex;

                return expression;
            }
            return null;
        }

        public ExpressionParser CodeBlockParser { get; set; }

    }
}
